package com.masterclient.app.businessmodule.vipmodule.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.design.widget.TabLayout.TabLayoutOnPageChangeListener;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.widget.CommonTabLayout;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.vipmodule.adapter.FragmentAdapter;
import com.masterclient.app.businessmodule.vipmodule.bean.Order;
import com.masterclient.app.businessmodule.vipmodule.fragment.OrderFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 零售订单管理
 */
public class SaleOrderActivity extends FCBusinessActivity {
    @BindView(R.id.tabLayout)
    CommonTabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private String target;
    private List<Fragment> fragments;
    private int currentItem;
    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_sale_order;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("零售订单管理");
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        target = getIntent().getStringExtra("target");
        initFragment();
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    /**
     * 初始化fragment
     */
    private void initFragment() {
        List<String> tabs = new ArrayList<>();
        tabs.add("全部");
        tabs.add("待付款");
        tabs.add("待发货");
        tabs.add("待收货");
//        tabs.add("已收货");
        tabs.add("已完成");
        tabs.add("已移交");
        for (int i = 0; i < tabs.size(); i++) {
            tabLayout.addTab(tabs.get(i), false);
        }
        fragments = new ArrayList<>();
        fragments.add(OrderFragment.newInstance(0, ""));
        fragments.add(OrderFragment.newInstance(0, "0"));
        fragments.add(OrderFragment.newInstance(0, "1"));
        fragments.add(OrderFragment.newInstance(0, "2"));
//        fragments.add(OrderFragment.newInstance(0,""));//已收货
        fragments.add(OrderFragment.newInstance(0, "8"));
        fragments.add(OrderFragment.newInstance(0, "-1"));
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments, tabs);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new
                TabLayoutOnPageChangeListener(tabLayout.getTabLayout()));
        tabLayout.setupWithViewPager(viewPager);
        if ("dealer_paid_count".equals(target))
            currentItem = 2;
        else if ("dealer_pend_count".equals(target))
            currentItem = 1;
        else if ("dealer_ship_count".equals(target))
            currentItem = 3;
        viewPager.setCurrentItem(currentItem, false);
        addOnTabSelectedListener();
    }

    private void addOnTabSelectedListener(){
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                currentItem = position;
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(Order order) {
        // 刷新订单列表
        OrderFragment orderFragment = (OrderFragment) fragments.get(currentItem);
        orderFragment.refresh();
    }
}
